package com.jay.base.ui

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.jay.media.kit.LogHelper

/**
 * @author xuzhou4
 * @date 2022/2/17
 */
abstract class BaseFragment : Fragment(), BaseUI {

    protected var TAG = LogHelper.makeLogTag(this::class.java)

    protected lateinit var mActivity: AppCompatActivity

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var rootView = rootView(container)
        if (rootView == null && layoutId() > 0) {
            rootView = inflater.inflate(layoutId(), null)
        }

        if (rootView == null) {
            throw IllegalArgumentException("please call onCreateView first !!!")
        }
        ARouter.getInstance().inject(this)

        initView(rootView)
        initData()

        return rootView
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        context.also { mActivity = it as AppCompatActivity }
        initParams()
    }

    override fun initParams() {

    }

    override fun rootView(container: ViewGroup?): View? = null

    override fun layoutId(): Int = 0

    override fun initView(view: View) {
    }

    override fun initData() {
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        Log.d(TAG, "onHiddenChanged() called with: hidden = $hidden")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause() called")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy() called")
    }

    override fun onDetach() {
        super.onDetach()
        Log.d(TAG, "onDetach() called")
    }
}